﻿using System.Web;
using CY_MVC.Core;
using CY_MVC.UrlRewrite;

namespace CY_MVC.HttpModules
{
    #region MvcModule

    //注册到
    //<system.web>
    //    <httpModules>
    //        <add name="MvcModule" type="CY_MVC.HttpModules.MvcModule" />
    //    </httpModules>
    //</system.web>
    //
    //<system.webServer>
    //  <modules runAllManagedModulesForAllRequests="true">
    //    <add name="MvcModule" preCondition="integratedMode" type="CY_MVC.HttpModules.MvcModule"/>
    //  </modules>
    //</system.webServer>
    public class MvcModule : IHttpModule
    {
        public void Init(HttpApplication p_App)
        {
            //攻击防御
            if (StaticConfig.DDosAttackBannedCount > 0 && StaticConfig.DDosAttackBennedSeconds > 0 &&
                StaticConfig.DDosAttackCheckPerSeconds > 0)
            {
                p_App.BeginRequest += DDosAttackHelper.Defense;
            }

            //UrlRewrite
            if (!string.IsNullOrEmpty(StaticConfig.UrlRewriteRulesPath))
            {
                p_App.BeginRequest += UrlRewriteHelper.Rewrite;
            }

            //UrlRemap
            if (!string.IsNullOrEmpty(StaticConfig.UrlRemapRulesPath))
            {
                p_App.PostResolveRequestCache += UrlRemapHelper.Remap;
            }

            //设置Handler
            p_App.ResolveRequestCache += ControllerContext.SetController;

            //执行Handler
            p_App.PostResolveRequestCache += ControllerContext.RunController;
        }

        public void Dispose()
        {
        }
    }

    #endregion
}
